package com.zhongyouyun.other.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.mapper.MenuMapper;
import com.zhongyouyun.admin.mapper.RoleMapper;
import com.zhongyouyun.admin.mapper.UserMapper;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.model.RoleModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.config.RedisConfig;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class MyRealm extends AuthorizingRealm {
    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RedisConfig redisConfig;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) SecurityUtils.getSubject().getPrincipal();

        //User user=userRepository.findByUserName(userName);
        //根据用户名查询出用户记录
        QueryWrapper<UserModel> userModelQueryWrapper = new QueryWrapper<UserModel>();
        userModelQueryWrapper.eq("user_name", userName);
        UserModel user = userMapper.selectOne(userModelQueryWrapper);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //List<Role> roleList=roleRepository.findByUserId(user.getId());
        List<RoleModel> roleList = roleMapper.findByUserId(user.getId());
        Set<String> roles = new HashSet<String>();
        if (roleList.size() > 0) {
            for (RoleModel role : roleList) {
                roles.add(role.getName());
                //List<Tmenu> menuList=menuRepository.findByRoleId(role.getId());
                //根据角色id查询所有资源
                List<MenuModel> menuList = menuMapper.selectMenusByRoleId(role.getId());
                for (MenuModel menu : menuList) {
                    info.addStringPermission(menu.getName()); // 添加权限
                }
            }
        }
        info.setRoles(roles);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String userName = (String) authenticationToken.getPrincipal();
        //根据用户名查询出用户记录
        QueryWrapper<UserModel> userModelQueryWrapper = new QueryWrapper<UserModel>();
        userModelQueryWrapper.eq("user_name", userName);
        UserModel user = userMapper.selectOne(userModelQueryWrapper);
        if (user != null) {
            AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user, user.getPassword(), user.getTrueName());
            return authcInfo;
        } else {
            return null;
        }
    }
}
